#include "serve.hpp"
#include "Route.hpp"
#include "ThreadPool.hpp"
#include <memory>
#include <iostream>
using namespace ThreadPoolModule;
using task_t = std::function<void()>; // 定义任务类型

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }

    uint16_t port = std::stoi(argv[1]);
    Enable_Console_Log_Strategy();
    // 1. 路由服务
    Route route;

    // 2. 线程池
    auto pool = ThreadPool<task_t>::GetInstance();

    // 3. 网络服务器对象 提供通信功能
    std::unique_ptr<serve> usvr = std::make_unique<serve>(port, [&pool, &route](int sockfd, const std::string &word, InetAddr &cli)

                                                          { // 1. 任务打包：将成员函数调用转换为无参数任务
                                                              task_t task = std::bind(&Route::MessageRoute, &route, sockfd, word, cli);
                                                              // 2. 异步投递：将任务放入线程池队列
                                                              pool->Equeue(task);
                                                          });
    usvr->Init();
    usvr->Start();

    return 0;
}